﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using WorkingTime.Classes.Model;

namespace WorkingTime.Classes.ViewModel
{
    public class DataView : NotifyPropertyChangedClass
    {
        private Project activeProject;
        private ICommand addProjectCommand;
        private ICommand setStartCommand;
        private ICommand setStopCommand;
        private ICommand deleteProjectCommand;
        public ObservableCollection<Project> Projects { get; set; }

        public DataView()
        {
            this.Projects = new ObservableCollection<Project>(Project.ReadProjects());
            this.ActiveProject = this.Projects.FirstOrDefault();
        }

        public Project ActiveProject
        {
            get { return activeProject; }
            set
            {
                activeProject = value;
                this.OnPropertyChanged(nameof(ActiveProject));
            }
        }

        private WorkTime ActiveWorkTime { get; set; }

        private void AddProject(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                if (this.Projects.Where(x => x.Name == name).ToList().Count == 0)
                {
                    var project = new Project(name);
                    this.Projects.Add(project);
                    this.ActiveProject = project;
                }
                else
                {
                    MessageBox.Show($"Project with the name \"{name}\" already exist!");
                }
            }
            else
            {
                MessageBox.Show("Project name can't be empty!");
            }
        }

        private void SetStart(object obj)
        {
            if (this.ActiveProject != null)
            {
                if (this.ActiveWorkTime != null)
                {
                    if (this.ActiveWorkTime.StartTime == null)
                    {
                        this.ActiveWorkTime.SetStartTime();
                    }
                }
                else
                {
                    this.ActiveWorkTime = new WorkTime();
                    this.ActiveWorkTime.SetStartTime();
                    this.ActiveProject.AddWorkTime(this.ActiveWorkTime);
                }
            }
        }

        private void SetStop(object obj)
        {
            if (this.ActiveProject != null)
            {
                if (this.ActiveWorkTime?.StartTime != null)
                {
                    this.ActiveWorkTime.SetStopTime();
                    this.ActiveWorkTime = null;
                    this.ActiveProject.RefreshTotalTime();
                }
            }
        }

        private void AddProject(object obj)
        {
            if (this.ActiveProject != null)
            {
                this.ActiveProject.DeleteProject();
                this.Projects.Remove(this.ActiveProject);
                this.ActiveProject = this.Projects.FirstOrDefault();
            }
        }

        public ICommand SetStartCommand
        {
            get
            {
                if (setStartCommand == null)
                {
                    setStartCommand = new RelayCommand<object>(this.SetStart);
                }
                return setStartCommand;
            }
        }

        public ICommand SetStopCommand
        {
            get
            {
                if (setStopCommand == null)
                {
                    setStopCommand = new RelayCommand<object>(this.SetStop);
                }
                return setStopCommand;
            }
        }

        public ICommand AddProjectCommand
        {
            get
            {
                if (addProjectCommand == null)
                {
                    addProjectCommand = new RelayCommand<string>(this.AddProject);
                }
                return addProjectCommand;
            }
        }

        public ICommand DeleteProjectCommand
        {
            get
            {
                if (deleteProjectCommand == null)
                {
                    deleteProjectCommand = new RelayCommand<object>(this.AddProject);
                }
                return deleteProjectCommand;
            }
        }
    }
}
